<template>
  <div>
    <h1>我的所有客户</h1>
    <el-table
      :data="pageData.data"
      style="width: 100%"
    >
      <el-table-column
        label="用户名"
        width="150"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        width="150"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用户类型"
        width="150"
      >
        <template slot-scope="scope">
          <span
            style="margin-left: 10px"
            v-if="scope.row.userType == 1"
          >{{
            '会员'
          }}</span>
          <span
            style="margin-left: 10px"
            v-else
          >{{ '普通用户' }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="联系电话"
        width="150"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="积分"
        width="150"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.score }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="$router.push(`/salesman/updateUser/${scope.row._id}`)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="deleteOneUser(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageData.currentPage"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageData.total"
    ></el-pagination>
  </div>
</template>

<script>
import api from "../../../network/apis/api";
export default {
  data() {
    return {
      pageData: {
        currentPage: 1,
        pageSize: 5,
        total: 0,
        totalPages: 5,
        data: [],
        _id: localStorage._id,
      },
    };
  },
  created() {
    this.myUsersList();
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    //删除用户
    async deleteOneUser(index, row) {
      // console.log(row._id);
      const { data } = await api.users.deleteOneUser(row._id);
      this.pageData._id = localStorage.getItem("_id");
      if (data.status) {
        await this.myUsersList();
        this.$message({
          message: "删除成功",
          type: "success",
        });
        // window.location.reload()
      }
    },
    //获取所有的用户
    async myUsersList() {
      const { data } = await api.users.myUsersList(this.pageData);
      // console.log(data);
      this.pageData = data.data;
    },

    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.getAllUsers();
    },
    handleCurrentChange(val) {
      this.pageData.currentPage = val;
      this.getAllUsers();
    },
  },
};
</script>

<style></style>
